package com.dzl.asynthread;

import com.dzl.card.dao.WelfareCardSecretMapper;
import com.dzl.card.pojo.WelfareCardSecret;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.WelfareGoodsEnum;
import com.dzl.enterprise.dao.WelfareEnterpriseMapper;
import com.dzl.enterprise.pojo.WelfareEnterprise;
import com.dzl.exchange.dao.WelfareExchangeSecretMapper;
import com.dzl.exchange.pojo.WelfareExchangeSecret;
import com.dzl.goods.dao.WelfareGoodsAttrMapper;
import com.dzl.goods.dao.WelfareGoodsClassifyMapper;
import com.dzl.goods.dao.WelfareGoodsImgMapper;
import com.dzl.goods.dao.WelfareGoodsMapper;
import com.dzl.goods.pojo.WelfareGoods;
import com.dzl.goods.pojo.WelfareGoodsAttr;
import com.dzl.goods.pojo.WelfareGoodsClassify;
import com.dzl.goods.pojo.WelfareGoodsImg;
import com.dzl.micro.pojo.WelfareMicroPage;
import com.dzl.micro.pojo.vo.WelfareMicroVo;
import com.dzl.micro.service.WelfareMicroPageService;
import com.dzl.moive.dao.WelfareMovieCinemaMapper;
import com.dzl.moive.dao.WelfareMovieCinemaRelMapper;
import com.dzl.moive.dao.WelfareMovieCityMapper;
import com.dzl.moive.pojo.WelfareMovieCinema;
import com.dzl.moive.pojo.WelfareMovieCinemaRel;
import com.dzl.moive.pojo.WelfareMovieCity;
import com.dzl.moive.pojo.vo.WelfareMovieOpiDetailVo;
import com.dzl.moive.utils.MovieUtils;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.dzl.util.QRCode;
import com.dzl.util.RedisUtil;
import com.dzl.util.wx.WxConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;

/**
 * 异步线程执行一些耗时的操作
 * @author liuguanjun
 * @since 2021-09-10 15:37:41
 */
@Service
public class SyncInitDataServiceImpl {

    private static final Logger logger = Logger.getLogger(SyncInitDataServiceImpl.class.getName());
    /*@Autowired
    private WelfareUserWxService welfareUserWxService;
    @Autowired
    private WelfareUserOrganizationMapper welfareUserOrganizationMapper;*/
    @Autowired
    private WelfareMovieCinemaMapper welfareMovieCinemaMapper;
    @Autowired
    private WelfareMovieCityMapper welfareMovieCityMapper;
    @Autowired
    private WelfareMovieCinemaRelMapper welfareMovieCinemaRelMapper;
    @Autowired
    private WelfareMicroPageService welfareMicroPageService;
    @Autowired
    private WelfareCardSecretMapper welfareCardSecretMapper;
    @Autowired
    private WelfareExchangeSecretMapper welfareExchangeSecretMapper;
    @Autowired
    private WelfareGoodsMapper welfareGoodsMapper;
    @Autowired
    private WelfareGoodsClassifyMapper welfareGoodsClassifyMapper;
    @Autowired
    private WelfareGoodsAttrMapper welfareGoodsAttrMapper;
    @Autowired
    private WelfareGoodsImgMapper welfareGoodsImgMapper;
    @Autowired
    private WelfareEnterpriseMapper welfareEnterpriseMapper;


    /**
     * 批量导入用户账号
     * @param list list数组
     * @param enterpriseId  企业id
     * @param userOrganizationDto  用户所属组织信息
     * @param setExists 该组织已存在的手机号
     * @param setCreats 本次导入已存在的手机号
     * @author liuguanjun
     * @since 2021-05-25 14:14:41
     */
    /*@Async
    public void addImportUserForOrganization(List<List<String>> list, Integer enterpriseId, UserOrganizationDto userOrganizationDto,Set setExists , Set setCreats) {
        logger.info("批量导入用户账号====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString());
        for (List<String> item : list) {
            String userName = item.get(0);
            String phoneNumber = item.get(1);
            //如果手机号已存在
            if (setExists.contains(phoneNumber)){
                //查询该用户信息
                WelfareUserWx welfareUserWxQuery = new WelfareUserWx();
                welfareUserWxQuery.setEnterpriseId(enterpriseId);
                welfareUserWxQuery.setPhone(phoneNumber);
                welfareUserWxQuery.setStatus(CommonConstants.YES);
                List<WelfareUserWx> welfareUserWxList_ = welfareUserWxService.select(welfareUserWxQuery);
                System.out.println("========================================================="+userName+"=="+phoneNumber);
                WelfareUserWx welfareUserWx = welfareUserWxList_.get(0);
                //查询该用户在此组织下是否存在
                WelfareUserOrganization welfareUserOrganizationQuery = new WelfareUserOrganization();
                welfareUserOrganizationQuery.setUserId(welfareUserWx.getId());
                welfareUserOrganizationQuery.setStatus(CommonConstants.YES);
                welfareUserOrganizationQuery.setEnterpriseId(enterpriseId);
                welfareUserOrganizationQuery.setFirstOrganizationId(userOrganizationDto.getFirstOrganizationId());
                welfareUserOrganizationQuery.setSecondOrganizationId(userOrganizationDto.getSecondOrganizationId());
                welfareUserOrganizationQuery.setThirdOrganizationId(userOrganizationDto.getThirdOrganizationId());
                welfareUserOrganizationQuery.setFourthOrganizationId(userOrganizationDto.getFourthOrganizationId());
                welfareUserOrganizationQuery.setFifthOrganizationId(userOrganizationDto.getFifthOrganizationId());
                List<WelfareUserOrganization> welfareUserOrganizationList = welfareUserOrganizationMapper.select(welfareUserOrganizationQuery);
                //如果不存在，则添加组织信息
                if(EmptyUtils.isEmpty(welfareUserOrganizationList)){
                    welfareUserOrganizationMapper.insertSelective(welfareUserOrganizationQuery);
                }
            }else{
                if(!setCreats.contains(phoneNumber)){
                    //添加用户
                    WelfareUserWx welfareUserWxAdd = new WelfareUserWx();
                    welfareUserWxAdd.setEnterpriseId(enterpriseId);
                    welfareUserWxAdd.setUserName(userName);
                    welfareUserWxAdd.setPassword(PasswordUtil.AESEncrypt("123456"));
                    welfareUserWxAdd.setPayPassword(PasswordUtil.AESEncrypt("1234"));
                    welfareUserWxAdd.setPhone(phoneNumber);
                    welfareUserWxAdd.setCreateTime(new Date());
                    welfareUserWxAdd.setStatus(1);
                    welfareUserWxService.insertSelective(welfareUserWxAdd);
                    // 将该账号加到过滤器
                    setCreats.add(phoneNumber);
                    //添加用户组织
                    WelfareUserOrganization userOrganizationAdd = new WelfareUserOrganization();
                    userOrganizationAdd.setEnterpriseId(enterpriseId);
                    userOrganizationAdd.setUserId(welfareUserWxAdd.getId());
                    userOrganizationAdd.setFirstOrganizationId(userOrganizationDto.getFirstOrganizationId());
                    if (EmptyUtils.isNotEmpty(userOrganizationDto.getSecondOrganizationId())) {
                        userOrganizationAdd.setSecondOrganizationId(userOrganizationDto.getSecondOrganizationId());
                    }
                    if (EmptyUtils.isNotEmpty(userOrganizationDto.getThirdOrganizationId())) {
                        userOrganizationAdd.setThirdOrganizationId(userOrganizationDto.getThirdOrganizationId());
                    }
                    if (EmptyUtils.isNotEmpty(userOrganizationDto.getFourthOrganizationId())) {
                        userOrganizationAdd.setFourthOrganizationId(userOrganizationDto.getFourthOrganizationId());
                    }
                    if (EmptyUtils.isNotEmpty(userOrganizationDto.getFifthOrganizationId())) {
                        userOrganizationAdd.setFifthOrganizationId(userOrganizationDto.getFifthOrganizationId());
                    }
                    welfareUserOrganizationMapper.insertSelective(userOrganizationAdd);
                }
            }
        }
    }*/


    /**
     * 同步平台商品到企业
     * @param enterpriseId 企业id
     * @author liuguanjun
     * @since 2022-02-16 16:52:41
     */
    @Async
    public void syncGoodsListToCompany(Integer enterpriseId) {
        logger.info("同步平台商品到企业====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   enterpriseId:" + enterpriseId);
        /*//删除原来的导入商品和导入分类
        WelfareEnterpriseGoods welfareEnterpriseGoodsDeleteParam = new WelfareEnterpriseGoods();
        welfareEnterpriseGoodsDeleteParam.setEnterpriseId(enterpriseId);
        welfareEnterpriseGoodsMapper.delete(welfareEnterpriseGoodsDeleteParam);*/
        //查询平台端的所有非草稿状态的商品
        Example exampleGoods=Example.builder(WelfareGoods.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andNotEqualTo("goodsStatus", WelfareGoodsEnum.GOODS_STATUS_TYPE_DRAFT.getCode())
                .andEqualTo("isJicai", CommonConstants.NO)
                .andEqualTo("syncNonSelfSupport", CommonConstants.YES)
                .andEqualTo("enterpriseId",CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)).build();
        List<WelfareGoods> goodsList = welfareGoodsMapper.selectByExample(exampleGoods);
        if(EmptyUtils.isNotEmpty(goodsList)){
            goodsList.forEach(x->{
                x.setEnterpriseId(enterpriseId);
                //x.setIsShow(CommonConstants.YES);
                x.setCreateTime(new Date());
                x.setUpdateTime(new Date());
                x.setPlatformId(x.getId());
                if(x.getGoodsStatus()==WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN.getCode()){
                    x.setGoodsStatus(WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN_PLATFORM.getCode());
                }
                x.setId(null);
            });
            //批量新增企业关联商品
            welfareGoodsMapper.insertList(goodsList);
        }
        //查询平台端的所有商品分类
        Example exampleClassify=Example.builder(WelfareGoodsClassify.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andEqualTo("enterpriseId", CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)).build();
        List<WelfareGoodsClassify> goodsClassifyList = welfareGoodsClassifyMapper.selectByExample(exampleClassify);
        if(EmptyUtils.isNotEmpty(goodsClassifyList)){
            goodsClassifyList.forEach(x->{
                x.setCreateTime(new Date());
                x.setUpdateTime(new Date());
                x.setEnterpriseId(enterpriseId);
                x.setPlatformId(x.getId());
                x.setId(null);
            });
            //批量新增企业关联商品分类
            welfareGoodsClassifyMapper.insertList(goodsClassifyList);
        }
        Example exampleGoodsAttr=Example.builder(WelfareGoodsAttr.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andEqualTo("enterpriseId", CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)).build();
        List<WelfareGoodsAttr> goodsAttrList = welfareGoodsAttrMapper.selectByExample(exampleGoodsAttr);
        if(EmptyUtils.isNotEmpty(goodsAttrList)){
            goodsAttrList.forEach(x->{
                x.setCreateTime(new Date());
                x.setUpdateTime(new Date());
                x.setEnterpriseId(enterpriseId);
                x.setPlatformAttrId(x.getId());
                x.setPlatformGoodsId(x.getGoodsId());
                x.setId(null);
            });
            //批量新增企业关联商品属性
            welfareGoodsAttrMapper.insertList(goodsAttrList);
        }
        //批量修改企业商家商品的三级分类
        welfareGoodsMapper.updateWelfareGoodsClassifyByEnterpriseId(enterpriseId);
        //修改商品分类的父分类
        Example exampleUpdateClassify=Example.builder(WelfareGoodsClassify.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andNotEqualTo("parentId",0)
                .andEqualTo("enterpriseId", enterpriseId)).build();
        List<WelfareGoodsClassify> goodsClassifyList_ = welfareGoodsClassifyMapper.selectByExample(exampleUpdateClassify);
        goodsClassifyList_.forEach(x->{
            WelfareGoodsClassify welfareGoodsClassifyQuery = new WelfareGoodsClassify();
            welfareGoodsClassifyQuery.setEnterpriseId(enterpriseId);
            welfareGoodsClassifyQuery.setPlatformId(x.getParentId());
            WelfareGoodsClassify wgc = welfareGoodsClassifyMapper.selectOne(welfareGoodsClassifyQuery);
            x.setParentId(wgc.getId());
            welfareGoodsClassifyMapper.updateByPrimaryKeySelective(x);
        });
        //新增微页面首页
        WelfareMicroPage welfareMicroPage = new WelfareMicroPage();
        welfareMicroPage.setEnterpriseId(enterpriseId);
        welfareMicroPage.setStatus(CommonConstants.YES);
        welfareMicroPage.setIsMovie(CommonConstants.NO);
        welfareMicroPage.setIsHome(CommonConstants.YES);
        welfareMicroPage.setName("首页");
        welfareMicroPage.setShowType(2);
        welfareMicroPage.setCreateTime(new Date());
        welfareMicroPage.setCreaterName("首页");
        welfareMicroPageService.insertSelective(welfareMicroPage);
        //新增电影专区微页面
        WelfareMicroPage welfareMicroMoviePage = new WelfareMicroPage();
        welfareMicroMoviePage.setEnterpriseId(enterpriseId);
        welfareMicroMoviePage.setStatus(CommonConstants.YES);
        welfareMicroMoviePage.setIsMovie(CommonConstants.YES);
        welfareMicroMoviePage.setIsHome(CommonConstants.NO);
        welfareMicroMoviePage.setName("电影专区");
        welfareMicroMoviePage.setShowType(2);
        welfareMicroMoviePage.setCreateTime(new Date());
        welfareMicroMoviePage.setCreaterName("系统创建");
        welfareMicroPageService.insertSelective(welfareMicroMoviePage);
    }

    /**
     * 平台端添加商品同步到企业
     * @param goodsId 商品id
     * @author liuguanjun
     * @since 2022-02-22 10:13:41
     */
    @Async
    public void syncAddGoodsToCompany(Integer goodsId) {
        logger.info("平台端添加商品同步到企业====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   goodsId:" + goodsId);
        //查询平台端的所有非草稿状态的商品
        Example exampleGoods=Example.builder(WelfareEnterprise.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andNotEqualTo("id", CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)
                .andEqualTo("isOpenapp", CommonConstants.YES)
                .andEqualTo("appRuleConfig", CommonConstants.YES)).build();
        List<WelfareEnterprise> enterpriseList = welfareEnterpriseMapper.selectByExample(exampleGoods);
        if(EmptyUtils.isNotEmpty(enterpriseList)){
            //查询新增商品主体信息
            WelfareGoods welfareGoods = welfareGoodsMapper.selectByPrimaryKey(goodsId);
            //查询新增商品属性
            WelfareGoodsAttr welfareGoodsAttrQuery = new WelfareGoodsAttr();
            welfareGoodsAttrQuery.setStatus(CommonConstants.YES);
            welfareGoodsAttrQuery.setGoodsId(goodsId);
            List<WelfareGoodsAttr> welfareGoodsAttrs = welfareGoodsAttrMapper.select(welfareGoodsAttrQuery);
            //查询新增商品图片
            WelfareGoodsImg welfareGoodsImgQuery = new WelfareGoodsImg();
            welfareGoodsImgQuery.setStatus(CommonConstants.YES);
            welfareGoodsImgQuery.setGoodsId(goodsId);
            List<WelfareGoodsImg> welfareGoodsImgs = welfareGoodsImgMapper.select(welfareGoodsImgQuery);
            enterpriseList.forEach(x->{
               welfareGoods.setEnterpriseId(x.getId());
               welfareGoods.setPlatformId(goodsId);
               welfareGoods.setId(null);
               welfareGoodsMapper.insertUseGeneratedKeys(welfareGoods);
               //同步更新商品分类
               welfareGoodsMapper.updateWelfareGoodsClassifyByGoodsIdAndEnterpriseId(x.getId(),welfareGoods.getId());
               //同步新增商品属性
               if(EmptyUtils.isNotEmpty(welfareGoodsAttrs)){
                   welfareGoodsAttrs.forEach(y->{
                       y.setGoodsId(welfareGoods.getId());
                       y.setPlatformGoodsId(goodsId);
                       y.setPlatformAttrId(y.getId());
                       y.setId(null);
                       y.setEnterpriseId(x.getId());
                   });
                   welfareGoodsAttrMapper.insertList(welfareGoodsAttrs);
               }
               //同步新增商品图片
               if(EmptyUtils.isNotEmpty(welfareGoodsImgs)){
                    welfareGoodsImgs.forEach(y->{
                        y.setGoodsId(welfareGoods.getId());
                        y.setPlatformGoodsId(goodsId);
                        y.setId(null);
                        y.setEnterpriseId(x.getId());
                    });
                    welfareGoodsImgMapper.insertList(welfareGoodsImgs);
               }
            });
        }
    }

    /**
     * 平台端修改商品信息同步到企业
     * @param goodsId 商品id
     * @author liuguanjun
     * @since 2022-02-22 16:52:41
     */
    @Async
    public void syncUpdateGoodsToCompany(Integer goodsId) {
        logger.info("平台端修改商品同步到企业====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   goodsId:" + goodsId);
        //查询平台端的所有非草稿状态的商品
        Example exampleGoods=Example.builder(WelfareEnterprise.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andNotEqualTo("id", CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)
                .andEqualTo("isOpenapp", CommonConstants.YES)
                .andEqualTo("appRuleConfig", CommonConstants.YES)).build();
        List<WelfareEnterprise> enterpriseList = welfareEnterpriseMapper.selectByExample(exampleGoods);
        if(EmptyUtils.isNotEmpty(enterpriseList)){
            //查询新增商品主体信息
            WelfareGoods welfareGoods = welfareGoodsMapper.selectByPrimaryKey(goodsId);
            if(EmptyUtils.isEmpty(welfareGoods.getSyncNonSelfSupport())){
                return;
            }
            //查询新增商品属性
            WelfareGoodsAttr welfareGoodsAttrQuery = new WelfareGoodsAttr();
            welfareGoodsAttrQuery.setStatus(CommonConstants.YES);
            welfareGoodsAttrQuery.setGoodsId(goodsId);
            List<WelfareGoodsAttr> welfareGoodsAttrs = welfareGoodsAttrMapper.select(welfareGoodsAttrQuery);
            //查询新增商品图片
            WelfareGoodsImg welfareGoodsImgQuery = new WelfareGoodsImg();
            welfareGoodsImgQuery.setStatus(CommonConstants.YES);
            welfareGoodsImgQuery.setGoodsId(goodsId);
            List<WelfareGoodsImg> welfareGoodsImgs = welfareGoodsImgMapper.select(welfareGoodsImgQuery);
            enterpriseList.forEach(x->{
                WelfareGoods welfareGoodsQuery = new WelfareGoods();
                welfareGoodsQuery.setEnterpriseId(x.getId());
                welfareGoodsQuery.setPlatformId(goodsId);
                WelfareGoods welfareEnterpriseGoods = welfareGoodsMapper.selectOne(welfareGoodsQuery);
                if(welfareEnterpriseGoods!=null){
                    welfareGoods.setEnterpriseId(x.getId());
                    welfareGoods.setPlatformId(goodsId);
                    welfareGoods.setId(welfareEnterpriseGoods.getId());
                    welfareGoodsMapper.updateByPrimaryKeySelective(welfareGoods);
                    //删除原商品属性
                    Example example = new Example(WelfareGoodsAttr.class);
                    example.createCriteria().andEqualTo("goodsId", welfareEnterpriseGoods.getId());
                    WelfareGoodsAttr welfareGoodsAttrUpd = new WelfareGoodsAttr();
                    welfareGoodsAttrUpd.setStatus(0);
                    welfareGoodsAttrMapper.updateByExampleSelective(welfareGoodsAttrUpd, example);
                    //删除原商品图片
                    WelfareGoodsImg welfareGoodsImgDel = new WelfareGoodsImg();
                    welfareGoodsImgDel.setGoodsId(welfareEnterpriseGoods.getId());
                    welfareGoodsImgMapper.delete(welfareGoodsImgDel);
                }else{
                    welfareGoods.setEnterpriseId(x.getId());
                    welfareGoods.setPlatformId(goodsId);
                    welfareGoods.setId(null);
                    welfareGoodsMapper.insertUseGeneratedKeys(welfareGoods);
                    //同步更新商品分类
                    welfareGoodsMapper.updateWelfareGoodsClassifyByGoodsIdAndEnterpriseId(x.getId(),welfareGoods.getId());
                }
                //同步新增商品属性
                if(EmptyUtils.isNotEmpty(welfareGoodsAttrs)){
                    welfareGoodsAttrs.forEach(y->{
                        y.setGoodsId(welfareGoods.getId());
                        y.setPlatformGoodsId(goodsId);
                        y.setPlatformAttrId(y.getId());
                        y.setId(null);
                        y.setEnterpriseId(x.getId());
                    });
                    welfareGoodsAttrMapper.insertList(welfareGoodsAttrs);
                }
                //同步新增商品图片
                if(EmptyUtils.isNotEmpty(welfareGoodsImgs)){
                    welfareGoodsImgs.forEach(y->{
                        y.setGoodsId(welfareGoods.getId());
                        y.setPlatformGoodsId(goodsId);
                        y.setId(null);
                        y.setEnterpriseId(x.getId());
                    });
                    welfareGoodsImgMapper.insertList(welfareGoodsImgs);
                }
            });
        }
    }

    /**
     * 平台端商品上下架同步状态到企业端
     * @param goodsId 商品id
     * @author liuguanjun
     * @since 2022-02-22 17:17:41
     */
    @Async
    public void syncUpdateGoodsStatusToCompany(Integer goodsId,Integer goodsStatus) {
        logger.info("平台端商品上下架同步状态到企业端====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   goodsId:" + goodsId+ "   goodsStatus:" + goodsStatus);
        //查询平台端的所有非草稿状态的商品
        Example exampleGoods=Example.builder(WelfareEnterprise.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andNotEqualTo("id", CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)
                .andEqualTo("isOpenapp", CommonConstants.YES)
                .andEqualTo("appRuleConfig", CommonConstants.YES)).build();
        List<WelfareEnterprise> enterpriseList = welfareEnterpriseMapper.selectByExample(exampleGoods);
        if(EmptyUtils.isNotEmpty(enterpriseList)){
            enterpriseList.forEach(x->{
                WelfareGoods welfareGoodsQuery = new WelfareGoods();
                welfareGoodsQuery.setEnterpriseId(x.getId());
                welfareGoodsQuery.setPlatformId(goodsId);
                if(goodsStatus == WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode()){
                    welfareGoodsQuery.setGoodsStatus(WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN_PLATFORM.getCode());
                }else if(goodsStatus == WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN.getCode()){
                    welfareGoodsQuery.setGoodsStatus(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode());
                }
                WelfareGoods welfareEnterpriseGoods = welfareGoodsMapper.selectOne(welfareGoodsQuery);
                if(welfareEnterpriseGoods!=null){
                    if(goodsStatus == WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode()){
                        welfareEnterpriseGoods.setGoodsStatus(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode());
                    }else if(goodsStatus == WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN.getCode()){
                        welfareEnterpriseGoods.setGoodsStatus(WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN_PLATFORM.getCode());
                    }
                    welfareGoodsMapper.updateByPrimaryKeySelective(welfareEnterpriseGoods);
                }
            });
        }
    }


    /**
     * 平台端添加商品分类同步到企业
     * @param classifyId 商品分类id
     * @author liuguanjun
     * @since 2022-02-22 10:13:41
     */
    @Async
    public void syncAddGoodsClassifyToCompany(Integer classifyId) {
        logger.info("平台端添加商品分类同步到企业====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   classifyId:" + classifyId);
        //查询平台端的所有非草稿状态的商品
        Example exampleGoods=Example.builder(WelfareEnterprise.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andNotEqualTo("id", CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)
                .andEqualTo("isOpenapp", CommonConstants.YES)
                .andEqualTo("appRuleConfig", CommonConstants.YES)).build();
        List<WelfareEnterprise> enterpriseList = welfareEnterpriseMapper.selectByExample(exampleGoods);
        if(EmptyUtils.isNotEmpty(enterpriseList)){
            //查询新增商品主体信息
            WelfareGoodsClassify welfareGoodsClassify = welfareGoodsClassifyMapper.selectByPrimaryKey(classifyId);
            Integer parentId = welfareGoodsClassify.getParentId();
            enterpriseList.forEach(x->{
                welfareGoodsClassify.setEnterpriseId(x.getId());
                welfareGoodsClassify.setPlatformId(classifyId);
                welfareGoodsClassify.setId(null);
                if(welfareGoodsClassify.getLevel()==1){
                    //welfareGoodsClassify.setParentId(0);
                }else{
                    WelfareGoodsClassify welfareGoodsClassifyQuery = new WelfareGoodsClassify();
                    welfareGoodsClassifyQuery.setStatus(CommonConstants.YES);
                    welfareGoodsClassifyQuery.setEnterpriseId(x.getId());
                    welfareGoodsClassifyQuery.setPlatformId(parentId);
                    WelfareGoodsClassify welfareGoodsClassify2 = welfareGoodsClassifyMapper.selectOne(welfareGoodsClassifyQuery);
                    if(EmptyUtils.isNotEmpty(welfareGoodsClassify2)){
                        welfareGoodsClassify.setParentId(welfareGoodsClassify2.getId());
                    }
                }
                welfareGoodsClassifyMapper.insertUseGeneratedKeys(welfareGoodsClassify);
            });
        }
    }


    /**
     * 缓存微页面
     * @param pageId 页面id
     * @author liuguanjun
     * @since 2021-12-21 18:22:41
     */
    @Async
    public WelfareMicroVo getMicroPageByPageId(Integer pageId) {
        logger.info("缓存微页面====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   pageId:" + pageId);
        WelfareMicroVo welfareMicroVo = welfareMicroPageService.getMicroPageByPageId(pageId, CommonConstants.DATA_PORT.APPLET);
        RedisUtil.set("microPage"+pageId, welfareMicroVo);
        return welfareMicroVo;
    }

    /**
     * 异步生成福利卡密的二维码
     * @param welfareCardSecretList 福利卡密数据列表
     * @author liuguanjun
     * @since 2022-01-12 10:22:41
     */
    @Async
    public void saveWelfareCardSecretQrcode(List<WelfareCardSecret> welfareCardSecretList) {
        logger.info("异步生成福利卡密的二维码====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   卡密数量:" + welfareCardSecretList.size());
        welfareCardSecretList.forEach(x->{
            //字符串每4位加个“-”
            String secretHasLine = x.getSecret().replaceAll("(.{4})", "$1-");
            //去掉最后一个“-”
            secretHasLine = secretHasLine.substring(0, secretHasLine.length() - 1);
            String qrcode = QRCode.getCardSecretQrCode(secretHasLine);
            if(EmptyUtils.isNotEmpty(qrcode)){
                x.setQrcode(qrcode);
                x.setUpdateTime(new Date());
                welfareCardSecretMapper.updateByPrimaryKeySelective(x);
            }
        });
    }

    /**
     * 异步生成兑换券密的二维码
     * @param welfareExchangeSecretList 兑换券密数据列表
     * @author liuguanjun
     * @since 2022-01-20 10:10:41
     */
    @Async
    public void saveWelfareExchangeSecretQrcode(List<WelfareExchangeSecret> welfareExchangeSecretList) {
        logger.info("异步生成兑换券密的二维码====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   券密数量:" + welfareExchangeSecretList.size());
        welfareExchangeSecretList.forEach(x->{
            String qrcode = ImageUtils.getWxCode(x.getNumber(), WxConfig.WX_XCX_APPID,WxConfig.WX_XCX_SECRET,"pages/quickExchange/index");
            if(EmptyUtils.isNotEmpty(qrcode)){
                x.setQrcode(qrcode);
                x.setUpdateTime(new Date());
                welfareExchangeSecretMapper.updateByPrimaryKeySelective(x);
            }
        });
    }

    /**
     * 同步更新电影院信息
     * @param citycode 城市编码
     * @param pageNo  当前页数
     * @param pageSize  每页条数
     * @param cityid 城市id主键
     * @author liuguanjun
     * @since 2021-11-03 09:26:41
     */
    @Async
    public void addMovieCinema(String citycode, Integer pageNo, Integer pageSize, Integer cityid) {
        logger.info("同步更新电影院信息====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString()+ "   pageNo:" + pageNo+ "   pageSize:" + pageSize+ "   citycode:" + citycode+ "   cityid:" + cityid);
        List<WelfareMovieCinema> list = MovieUtils.getMovieCinemaList(citycode,pageNo,pageSize);
        if(EmptyUtils.isNotEmpty(list)){
            List<WelfareMovieCinema> addlist = new ArrayList<>();
            for (WelfareMovieCinema ciname:list) {
                WelfareMovieCinema query = new WelfareMovieCinema();
                query.setId(ciname.getId());
                int count = welfareMovieCinemaMapper.selectCount(query);
                //WelfareMovieCinema cinema_ = welfareMovieCinemaMapper.selectByPrimaryKey(ciname.getId());
                if(count==0){
                    ciname.setCreateTime(new Date());
                    ciname.setUpdateTime(new Date());
                    addlist.add(ciname);
                }
            }
            if(EmptyUtils.isNotEmpty(addlist)){
                welfareMovieCinemaMapper.insertMovieCinemaList(addlist);
            }
            if(list.size()<pageSize){
                WelfareMovieCity welfareMovieCity = new WelfareMovieCity();
                welfareMovieCity.setId(cityid);
                welfareMovieCity.setIsupdate(CommonConstants.YES);
                welfareMovieCityMapper.updateByPrimaryKeySelective(welfareMovieCity);
                //RedisUtil.set("snycMovieCinema"+citycode, citycode,60*60*2);//缓存2小时
            }
        }else{
            WelfareMovieCity welfareMovieCity = new WelfareMovieCity();
            welfareMovieCity.setId(cityid);
            welfareMovieCity.setIsupdate(CommonConstants.YES);
            welfareMovieCityMapper.updateByPrimaryKeySelective(welfareMovieCity);
        }
    }

    /**
     * 同步区县信息
     * @param citylist 城市列表
     * @author liuguanjun
     * @since 2021-11-04 09:21:41
     */
    @Async
    public void addCountyList(List<WelfareMovieCity> citylist) {
        logger.info("同步区县信息====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString());
        List<WelfareMovieCity> addlist = new ArrayList<>();
        for (WelfareMovieCity city:citylist) {
            List<WelfareMovieCity> countylist = MovieUtils.getMovieCountyList(city.getCitycode());
            if(EmptyUtils.isNotEmpty(countylist)){
                for (WelfareMovieCity county:countylist) {
                    county.setCitycode(county.getId().toString());
                    county.setParentcode(city.getCitycode());
                    addlist.add(county);
                }
            }
        }
        if(EmptyUtils.isNotEmpty(addlist)){
            welfareMovieCityMapper.insertList(addlist);
        }
    }

    /**
     * 同步更新电影上映影院关联数据
     * @param movieList 电影列表
     * @param cinemaid 影院id
     * @author liuguanjun
     * @since 2021-11-04 09:21:41
     */
    @Async
    public void addMovieCanemaRel(Integer cinemaid, List<WelfareMovieOpiDetailVo> movieList) {
        logger.info("同步更新电影上映影院关联数据====>" + Thread.currentThread().getName() + "   " + UUID.randomUUID().toString());
        //String[] cinemaids_ = cinemaids.split(",");
        //List<Integer> list = welfareMovieCinemaRelMapper.selectWelfareMovieRelCinemaIds(movieid);
        List<WelfareMovieCinemaRel> relList = new ArrayList<>();
        for (WelfareMovieOpiDetailVo opi:movieList) {
            WelfareMovieCinemaRel relQuery = new WelfareMovieCinemaRel();
            relQuery.setCinemaid(cinemaid);
            relQuery.setMovieid(Integer.valueOf(opi.getMovieid()));
            int count = welfareMovieCinemaRelMapper.selectCount(relQuery);
            if(count == 0){
                WelfareMovieCinemaRel rel = new WelfareMovieCinemaRel();
                rel.setCinemaid(cinemaid);
                rel.setMovieid(Integer.valueOf(opi.getMovieid()));
                BigDecimal price = new BigDecimal(opi.getOriginalPrice()).divide(new BigDecimal(100));
                rel.setPrice(price);
                rel.setCreateTime(new Date());
                rel.setUpdateTime(new Date());
                relList.add(rel);
            }
        }
        if(EmptyUtils.isNotEmpty(relList)){
            welfareMovieCinemaRelMapper.insertList(relList);
        }
    }



}
